trigger CXGFetchEventOwnerManager on Event (before Insert,before Update)
 {
 
    //Trigger Name:CXGFetchEventOwnerManager
    //Date:4th August 2011  
    //Author:Manish Tiwari/Accenture  
    //Requirement/Project Name:CXG 
    //Requirement/Project Description:The Triggers fire on Insert or Update and if Owner has Changed.It Updates the Onwer's Manager Name. 
  
  Set<Id> user_id = new Set<Id>();
  ID CxGEventRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('Event', 'CXG_Record_Type');
  ID OOREventRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('Event', 'Local_Advertiser_Event');
   
     
     for(Event evt :Trigger.New) 
     {
       if(Trigger.isUpdate)
          {  
                 
                 Event evnt = Trigger.OldMap.get(evt.Id);
                 if((evt.RecordTypeId==CxGEventRecordTypeID || evt.RecordTypeId==OOREventRecordTypeID)&& evnt.OwnerId!=evt.Ownerid)
                 {  
                     user_id.add(evt.OwnerId);
                 }
           } 
        if(Trigger.isInsert)
           {
                if(evt.RecordTypeId==CxGEventRecordTypeID || evt.RecordTypeId==OOREventRecordTypeID)
                   {
                    user_id.add(evt.OwnerId);
                   }
           
           }   
     }     
     if(user_id.size()>0)
     {
     Map<Id,User> usrs = New Map<Id,User>(); 
         for(User usr:[Select Name,ManagerId,Manager.FirstName,Manager.LastName,SFA_Office__c from User where Id IN: user_id ] ) 
         {      
             if(usr.ManagerId!=null){
             usrs.put(usr.Id,usr); 
             } 
         }  
       if(usrs.Size()>0)
        {      
         for(Event evt :Trigger.New) 
           {
             evt.SFA_Office_Code__c=usrs.get(evt.OwnerId).SFA_Office__c; 
             ID usrMgr= usrs.get(evt.OwnerId).ManagerId; 
             evt.Owner_Manager__c=usrs.get(evt.OwnerId).Manager.FirstName+''+usrs.get(evt.OwnerId).Manager.LastName;
               
             } 
           }      
      }
 }